<?php
Class UserModel extends CI_Model
{
	// Attributes
	var $dni;
	var $username;
	var $group;

    function __construct()
    {
        parent::__construct();
    }

	function authenticate($username, $password)	{        
		$this->db->select('dni, username,group');
		$this->db->from('users');
		$this->db->where('username', $username);
		$this->db->where('password', hash('sha256', $password));
		$this->db->limit(1);

		$query = $this->db-> get();

   		if($query -> num_rows() == 1)
   		{
			$rows = $query->result();
			$this->dni = $rows[0]->dni;
			$this->username = $rows[0]->username;
			$this->group = $rows[0]->group;
			return $this;
   		}
        return FALSE;
 	}
	
	function obtenerUsuarios() {
		
		$query = $this->db->query("SELECT * FROM users");
		if($query->num_rows()>0) {
			foreach ($query->result_array() as $row)
			{
				$usuarios[] = $row;
			}
		}
		
		return $usuarios;
	}
	
	function perfilUsuario($data) {
		$this->db->from('users');
		$this->db->where('username', $data);
		$this->db->limit(1);

		$query = $this->db-> get();

   		if($query -> num_rows() == 1){
			$rows = $query->result();
			$this->username = $rows[0]->username;
			$this->password = $rows[0]->password;
			$this->email = $rows[0]->email;
			$this->name = $rows[0]->name;
			$this->last_name = $rows[0]->last_name;
			$this->dni = $rows[0]->dni;
			$this->address = $rows[0]->address;
			$this->zip = $rows[0]->zip;
			$this->town = $rows[0]->town;
			$this->province = $rows[0]->province;
			$this->sex = $rows[0]->sex;
			$this->phone = $rows[0]->phone;
			$this->birth_date = $rows[0]->birth_date;
			
			
			return $this;
   		}
        return FALSE;
	}
	
}
?>
